package edu.ccdb;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Vector;

import edu.ccdb.irods.InstanceInfo;
import edu.ucsd.ccdb.uploadV4.dataModel.CCDBFile;

public class Submitter {
	

    public static String urlPath = "UploadTool_V4_Services/submitUploadedFiles";
    
    public static InstanceInfo setUpSession ( String sessionID, String hostAndPortNumber ) {
    	String path = null;
    	Integer mpid = null;
    	
    	try
        {
    		//   URL url = new URL("http://localhost/UploadTool_V4_Services/convertSession");
//    		URL url = new URL("http://ccdb-stage-portal.crbs.ucsd.edu:8081/UploadTool_V4_Services/convertSession");
    		URL url = new URL ( hostAndPortNumber +  "UploadTool_V4_Services/convertSession");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                   
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod( "POST" );
            ObjectOutputStream outputToServlet = new ObjectOutputStream(conn.getOutputStream());

//            outputToServlet.writeObject("gqipjiok10_15_2009_4_43_13PM");
            outputToServlet.writeObject(sessionID);

            ObjectInputStream inputFromServlet = new ObjectInputStream(conn.getInputStream());
            Object o1 = inputFromServlet.readObject();
            Object o2 = inputFromServlet.readObject();
                               
        	mpid = (Integer)o1;
            path = (String)o2;
                               
            System.out.println( "RETURN: "+mpid+"  "+path);

            outputToServlet.flush();
            outputToServlet.close();
            
            inputFromServlet.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
         }
        
        return new InstanceInfo(path, mpid);
    }
    
	public static Integer submitInfoAboutUploadedFiles ( Vector<CCDBFile> inputVector, Integer mpID,  String hostAndPortNumber ){
		
		Integer result = null;
		try
	    {
			URL url;
			
//			if ( submitArgumentString != null ) 
//				url = new URL (submitArgumentString);
//			else 
				url = new URL( hostAndPortNumber + urlPath);
			
			System.out.println ("url submitted: " + url.getHost());
			
	        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
	                   
        	conn.setDoOutput(true);
	        conn.setDoInput(true);
	        conn.setRequestMethod( "POST" );
	        ObjectOutputStream outputToServlet = new ObjectOutputStream(conn.getOutputStream());

	        outputToServlet.writeObject( mpID );  //MPiD
	        outputToServlet.writeObject(inputVector);   //Vector of CCDBFile
	                                
	        outputToServlet.flush();
	        outputToServlet.close();

	        // testing, get input from stream first 
	        InputStream inputStream = conn.getInputStream(); 
	        ObjectInputStream inputFromServlet = new ObjectInputStream( inputStream );

//	        Integer o1 = (Integer) inputFromServlet.readObject();
	        Integer o2 = (Integer) inputFromServlet.readObject();
	        
	        System.out.println ( "return: "  + o2);
	        
	        result = o2; 
            
	        inputStream.close();
	    }
	    catch(Exception e)
	    {
	    	e.printStackTrace();
	    }
		return result;
	}
}
